
<template>
    <theme-box title="绑定营销规则">
        <form class="form-inline" @submit.prevent v-el:form>
            <div class="box-body">
                <form-group title="营销分钱规则" :required="false" >
                    <select name="marketRule" class="form-control">
                        <option value="">无</option>
                        <option v-for="rule in ruleList" :value="rule.value">
                            {{rule.key}}
                        </option>
                    </select>
                </form-group>
                <form-group title="营销结算分钱规则" :required="false" id="sett" v-show="this.thirdType == 'default'&& this.couponType != 'present' && this.couponType != 'ticket'">
                    <select name="settRule" class="form-control">
                        <option value="">无</option>
                        <option v-for="rule in useRuleList" :value="rule.value">
                            {{rule.key}}
                        </option>
                    </select>
                </form-group>
            </div>
            <div class="box-footer">
                <v-button type="reset" class="cancel">取消</v-button>
                <v-button type="submit" theme="primary" class="pull-right">确认</v-button>
            </div>
        </form>
    </theme-box>
</template>

<script>
    import 'plugins/jquery-validation';
    import 'lib/validatorByte.js';
    var serialize = require('form-serialize');


    export default {
        data: () => ({
            thirdType:'',
            couponType:'',
            ruleList:[],
            useRuleList:[],
        }),
        methods: {
            reset() {
                $("label.error").remove();
                this.$els.form.reset()
            },
            getRule(data) {
                let self = this;
                var _data =  localStorage.getItem("params");
                self.$post({
                    url: "/api/v2" + projectName_Url + "/plat/rule/listRules",
                    data:_data,
                    method: "post"
                }).then(response => {
                    let list = response.content.rulesList;
                    self.$set("ruleList", list);

                }, error => {
                });
            },
            getUseRule(data) {
                let self = this;
                var _data =  localStorage.getItem("params");
                self.$post({
                    url: "/api/v2" + projectName_Url + "/plat/rule/listUseRules",
                    data:_data,
                    method: "post"
                }).then(response => {
                    let list = response.content.rulesList;
                    self.$set("useRuleList", list);
                }, error => {
                });
            }
        },
        ready() {
            var self = this;
            self.getRule();
            self.getUseRule();
            $('.cancel').click(function(){
                self.$route.router.go('/coupons/list');
            });
            self.thirdType = JSON.parse(localStorage.getItem("params")).thirdType;
            self.couponType = JSON.parse(localStorage.getItem("params")).couponType;
//           if(thirdType == 'third'){
//               console.log(1)
//             $('#sett').css('display','none');
//           }else{
//             $('#sett').css('display','block');
//           }
            $(this.$els.form).validate({
                submitHandler: async function() {
                    var _data = serialize(this.currentForm, {
                        hash: true,
                        empty: true
                    });
//                    _data.mchtId = JSON.parse(localStorage.getItem("params")).mchtId;
                    _data.mchtId = parseInt(JSON.parse(localStorage.getItem("params")).mchtId);
                    _data.couponId = JSON.parse(localStorage.getItem("params")).couponId;
                    _data.couponNum = JSON.parse(localStorage.getItem("params")).num;
                    _data.validatyEnd = JSON.parse(localStorage.getItem("params")).validatyEnd;
                    self.$post({
                        url: "/api/v2"+ projectName_Url +"/plat/rule/boundCouponRule",
                        data: _data,
                        method: "post"
                    }).then(response => {
                        let data = {
                            title: "操作成功",
                            content: "是否进入电子券列表查看?"
                        }
                        self.reset();
//                        localStorage.remove('params');
                        Midway.confirm(data).then(function () {
                            self.$route.router.go('/coupons/list');
                        }).catch(function(){
                            self.$route.router.go('/coupons/list');
                        })
                    });
                },
            });
        }
    }
</script>
